﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using Maticsoft.Common;
using CustomsSystem.Model;
using CustomsSystem.BLL;

namespace WEB.sheetInfo
{
    public partial class sheetWorkflowStart : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        SheetInfoManage bll = new SheetInfoManage();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
                this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
                tbTimestart.Text = DateTime.Now.AddDays(-7).ToShortDateString();
                tbTimestop.Text = DateTime.Now.AddDays(1).ToShortDateString();

                if (Request.QueryString["ID"] != null)
                {
                    int id = Convert.ToInt32(Request.QueryString["ID"]);
                    ds = bll.GetView(" sheetID = " + id);
                }
                else
                {
                    ds = bll.GetView(Query());
                }

                gvWorkFlowStart.DataSource = ds;
                gvWorkFlowStart.DataBind();
            }
            else
            {
                ds = bll.GetView(Query());
                gvWorkFlowStart.DataSource = ds;
            }


        }
        /// <summary>
        /// 启动工作流
        /// </summary>
        public void WorkflowStart(int sheetNo)
        {
            //获取所需数据
            ds = bll.GetWorkFlowStart(" sheetID =" + sheetNo);//对应流程，详细信息表的信息
            CustomsSystem.Model.workflow flowModel = new CustomsSystem.Model.workflow();
            //判断是报关 、报检 还是 都是
            CustomsSystem.Model.sheetInfo mysheet = bll.GetModel(sheetNo);
            if (mysheet.InfoWKTypeIDbg != -1)
            {
                //T_workflow表数据的录入
                flowModel.sheetID = sheetNo;
                flowModel.busID = 1;
                flowModel.busName = ds.Tables[0].Rows[0]["busNamebg"].ToString();
                flowModel.jobNO = ds.Tables[0].Rows[0]["jobNobg"].ToString();
                flowModel.inputEmpNo = Session["EmpNo"].ToString();
                flowModel.inputDate = DateTime.Now;
                flowModel.receiveMark = 0;
                if (ds.Tables[0].Rows[0]["infowkTypeIDbg"].ToString() == "")
                {
                    MessageBox.Show(this, "数据出错，启动失败!");
                    return;
                }
                flowModel.infoWKID = Convert.ToInt32(ds.Tables[0].Rows[0]["infowkTypeIDbg"].ToString());//模板编号
                WorkflowHandleManage flowManage = new WorkflowHandleManage();//模板表
                //委托单变成在用状态
                WorkflowManage flowManageM = new WorkflowManage();//T_workflow流程表
                int masterID = 0 ;
                if (flowModel.infoWKID != null) masterID = (int)flowModel.infoWKID;//模板编号
                CustomsSystem.Model.Info_workflowMaster masterModel = flowManageM.GetModelMaster(masterID);
                masterModel.States = 1;//启动模板，不能修改
                flowManageM.UpdateMaster(masterModel);
                //=====================================
                int newid = flowManage.Add(flowModel);//增加T_workflow
                //委托单的冗余字段修改
                mysheet.busIDbg = 1;
                mysheet.busNamebg = ds.Tables[0].Rows[0]["busNamebg"].ToString();
                mysheet.jobNobg = ds.Tables[0].Rows[0]["jobNobg"].ToString();
                mysheet.infoWKIDbg = newid;
            }
            if (mysheet.InfoWKTypeIDbj != -1)//报检的判断，现在不启用
            {
                flowModel.sheetID = sheetNo;
                flowModel.busID = 1;
                flowModel.busName = ds.Tables[0].Rows[0]["busNamebj"].ToString();
                flowModel.jobNO = ds.Tables[0].Rows[0]["jobNobj"].ToString();
                flowModel.inputEmpNo = Session["EmpNo"].ToString();
                flowModel.inputDate = DateTime.Now;
                flowModel.receiveMark = 0;

                if (ds.Tables[0].Rows[0]["infowkTypeIDbj"].ToString() == "")
                {
                    MessageBox.Show(this, "数据出错，启动失败!");
                    return;
                }
                flowModel.infoWKID = Convert.ToInt32(ds.Tables[0].Rows[0]["infowkTypeIDbj"].ToString());

                WorkflowHandleManage flowManage = new WorkflowHandleManage();
                int newid2 = flowManage.Add(flowModel);
                //插入委托单工作流信息表数据
                mysheet.busIDbj = 1;
                mysheet.busNamebj = ds.Tables[0].Rows[0]["busNamebj"].ToString();
                mysheet.jobNobj = ds.Tables[0].Rows[0]["jobNobj"].ToString();
                mysheet.infoWKIDbj = newid2;
            }
            bll.Update(mysheet);//更新委托单表，完成操作
        }
        /// <summary>
        /// 启动按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            //进行CommandArgument的绑定后，读取ID，确认窗口在页面层实现；
            string id = ((Button)sender).CommandArgument.ToString();
            WorkflowStart(Convert.ToInt32(id));
            MessageBox.ShowAndRedirect(this, "启动成功", "sheetWorkflowStart.aspx");
        }

        protected void gvWorkFlowStart_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvWorkFlowStart.PageIndex = e.NewPageIndex;
            gvWorkFlowStart.DataBind();
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gvWorkFlowStart.DataBind();
        }
        /// <summary>
        /// 搜索判断
        /// </summary>
        /// <returns></returns>
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " busIDbg is null and busIDbj is null ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    default: if (!PageValidate.IsNumber(tbQuery.Text.Trim())) tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and inputDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and inputDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            strSql += " order by sheetID  " + ddlasc.SelectedValue;
            return strSql;
        }

        protected void ddlasc_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvWorkFlowStart.DataBind();
        }

    }
}